<template>
  <div id="app">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <p>登录</p>
      <el-form-item label="账号" prop="name">
        <el-input v-model.number="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <div class="btn">
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录
        </el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
import http from "axios";

import { checkLogin } from "../api/user";

export default {
  data() {
    var checkAge = (rule, value, callback) => {
      let reg = /^\w{3,12}$/;
      if (!reg.test(value)) {
          callback("账号长度为3~12位");
      }
        callback()//成功后往后执行
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } 
      callback();//成功后往后执行
    };
    return {
      ruleForm: {
        pass: "",
        name: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        name: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: { 
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = {
            username: this.ruleForm.name,//数据库的参数名 
            pass: this.ruleForm.pass,
          };
          console.log(params)
          localStorage.setItem('admin',this.ruleForm.name)
            http.post("/api/administrator/checklogin", params).then((res) => {
              // 判断接口状态
              console.log(res)
              const {errCode, errMsg,  data} = res.data
              if (errCode == 10000) {
                // 存储token
                sessionStorage.setItem("token", data.token);
                console.log(this.$store.commit('menu/SET_LIST'))
                this.$store.commit('menu/SET_LIST', data.menu)
                // 路由跳转
                this.$router.push("/index");
                // 登录成功提示
                this.$message.success("登录成功");
              } else {
                this.$message.error(errMsg);
              }
            });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script> 
<style scoped>
#app{
    height: 100%;
   background-image: url(./../assets/images/meinv.jpg);
}
.demo-ruleForm {
  width: 30%;
  position: absolute;
  background-color: #fff;
  border-radius: 5px;
  padding-bottom: 25px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
p {
  width: 100%;
  margin-top: 30px;
  /* height: */
  font-weight: 900;
  text-align: center;
}
.btn {
  width: 100%;
  text-align: Center;
}
.el-input {
  width: 80%; 
}
</style>

